package com.mytaobao.main.firsttab;


//import static com.google.common.base.Preconditions.checkNotNull;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baseproject.mvp.MvpFragment;
import com.baseproject.tangram.ITangramContainer;
import com.baseproject.tangram.TangramHelper;
import com.baseproject.view.banner.BannerViewPager;
import com.jaeger.library.StatusBarUtil;
import com.mytaobao.R;
import com.mytaobao.main.firsttab.func.chaoshihui.ItemChaoShiHuiView;
import com.mytaobao.main.firsttab.func.flashsale.FlashSaleViewLRClock;
import com.mytaobao.main.firsttab.func.flashsale.FlashSaleViewMiddleTitle;
import com.mytaobao.main.firsttab.func.flashsale.FlashSaleViewTB;
import com.mytaobao.main.firsttab.func.guessyoulike.GuessYouLikeView;
import com.mytaobao.main.firsttab.func.guessyoulike.ItemGuessYouLike;
import com.mytaobao.main.firsttab.func.headline.HeadLineVerticalViewPager;
import com.mytaobao.main.firsttab.func.headline.HeadlineView;
import com.mytaobao.main.firsttab.func.menu.MenuItemView;
import com.mytaobao.view.TangramImageView;
import com.tmall.wireless.tangram.TangramBuilder;
import com.tmall.wireless.tangram.TangramEngine;
import com.tmall.wireless.tangram.dataparser.concrete.Card;
import com.tmall.wireless.tangram.structure.BaseCell;
import com.tmall.wireless.tangram.support.SimpleClickSupport;
import com.tmall.wireless.tangram.support.async.AsyncLoader;
import com.tmall.wireless.tangram.support.async.AsyncPageLoader;
import com.tmall.wireless.tangram.support.async.CardLoadSupport;

import java.util.List;

import butterknife.BindView;

/**
 * 将View分离为基础方法和业务方法
 */
public class FirstTabFragment extends MvpFragment<FirstTabFragment, FirstTabPresenter>
        implements FirstTabContract.IFirstTabView, ITangramContainer {

    @BindView(R.id.rv_firsttab)
    RecyclerView recyclerView;

    private FirstTabRecyclerAdapter firstTabRecyclerAdapter;
    HeadLineVerticalViewPager mHeadLineVerticalViewPager;
    public TangramHelper mTangramHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_firsttab;
    }

    @NonNull
    @Override
    public FirstTabPresenter createPresenter() {
        return new FirstTabPresenter();
    }

    @Override
    protected void _onCreateView(View view, ViewGroup container, Bundle savedInstanceState) {
        if(null == mTangramHelper) {
            mTangramHelper = new TangramHelper().initTangram(this.getContext(), this);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public void initStatusBar() {
        StatusBarUtil.setColor(this.getActivity(), getResources().getColor(R.color.colorPrimary));
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public int getRVScrollState() {
        return recyclerView.getScrollState();
    }

    @Override
    public void showLikeItems(List<ItemGuessYouLike> itemGuessYouLikes) {
//        firstTabRecyclerAdapter.setDataGuessYouLike(itemGuessYouLikes);
    }


    @Override
    public boolean getIfEnableTangram() {
        return true;
    }

    @Override
    public void registerCellOrCard(TangramBuilder.InnerBuilder builder) {
        builder.registerCell(1, MenuItemView.class);
        builder.registerCell(2, BannerViewPager.class);
        builder.registerCell(3, HeadlineView.class);
        builder.registerCell(4, FlashSaleViewLRClock.class);
        builder.registerCell(7, FlashSaleViewMiddleTitle.class);
        builder.registerCell(8, FlashSaleViewTB.class);
        builder.registerCell(5, ItemChaoShiHuiView.class);
        builder.registerCell(6, TangramImageView.class);
        builder.registerCell(9, GuessYouLikeView.class);
    }

    AsyncLoader firstTabAsyncLoader = new AsyncLoader() {
        @Override
        public void loadData(Card card, @NonNull LoadedCallback callback) {
            Log.w("Load Card", card.load);
        }
    };

    AsyncPageLoader firstTabAsyncPageLoader = new AsyncPageLoader() {
        @Override
        public void loadData(int page, @NonNull Card card, @NonNull LoadedCallback callback) {
            Log.w("Load page", card.load + " page " + page);
        }
    };

    @Override
    public void addSupportAndRegisterService(TangramEngine engine) {
        engine.addCardLoadSupport(new CardLoadSupport(firstTabAsyncLoader, firstTabAsyncPageLoader));
        engine.addSimpleClickSupport(new SampleClickSupport());
    }

    public class SampleClickSupport extends SimpleClickSupport {

        public SampleClickSupport() {
            setOptimizedMode(true);
        }

        @Override
        public void defaultClick(View targetView, BaseCell cell, int type) {
            super.defaultClick(targetView, cell, type);
            Toast.makeText(targetView.getContext(), " 您点击了组件  type:"+cell.type, Toast.LENGTH_SHORT).show();
        }
    }
}
